package com.ui.holder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.core.sdk.core.LogUtil;
import com.core.sdk.extra.task.ImageLoadTask;
import com.core.sdk.extra.task.ImageLoadTask.OnImageLoadFinishCallBack;
import com.core.sdk.ui.holder.BaseViewHolder;
import com.core.sdk.ui.waterfall.internal.PLA_AbsListView.OnScrollListener;
import com.core.sdk.utils.ScreenUtil;
import com.ui.R;
import com.ui.model.Shop;

public class ShopHolder extends BaseViewHolder<Shop, Void> implements OnImageLoadFinishCallBack {

	private static final String TAG = ShopHolder.class.getSimpleName();

	ImageView iv;
	TextView tv;
	Shop oldItem = null;
	ImageLoadTask task = null;

	final int STATUS_IDLE = OnScrollListener.SCROLL_STATE_IDLE;

	@Override
	public void onScrollStateChanged(ViewGroup vg, int scrollState) {
		super.onScrollStateChanged(vg, scrollState);
		if (scrollState == STATUS_IDLE) {
			// 检查当前holder是否在屏幕上显示
			if (getPosIndex() >= firstVisibleItem - getHeaderCount() && getPosIndex() <= visibleItemCount + firstVisibleItem) {
				bindImage();
			}
		}
	}

	public ShopHolder(View rootView, Context act) {
		super(rootView, act);
		init(act);
	}

	@Override
	protected void onInitViews(View view) {
		iv = find(R.id.item_shop_iv);
		tv = find(R.id.item_shop_tv);
	}

	@Override
	protected void onBindItem() {
		bindText();
		setImageViewHeight();
		clearImageView();
		if (this.scrollState == STATUS_IDLE) {
			bindImage();
		}else{
			clearImageView();
		}
	}

	/** 根据图片大小预先设置好ImageView的大小 */
	private void setImageViewHeight() {
		// (1)获得图片大小
		String imgUrl = getItem().getData().getImgUrl();
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imgUrl, opts);
		int bmpWidth = opts.outWidth;
		int bmpHeight = opts.outHeight;
		// (2)获得itemView的宽度
		int itemPadding = getRootView().getPaddingLeft() + getRootView().getPaddingRight();
		int newImageViewWidth = itemWidth - itemPadding;
		int newImageViewHeight = Math.round(bmpHeight * 1f * newImageViewWidth / bmpWidth);
		LayoutParams iv_lp = iv.getLayoutParams();
		if (iv_lp == null) {
			iv_lp = new LayoutParams(newImageViewWidth, newImageViewHeight);
		} else {
			iv_lp.width = newImageViewWidth;
			iv_lp.height = newImageViewHeight;
		}
		// (3)设置ImageView的大小
		iv.setLayoutParams(iv_lp);
		// LogUtil.e(TAG, "bmp.width=" + bmpWidth + " bmp.height=" + bmpHeight);
		// LogUtil.e(TAG, "item.width=" + itemWidth +
		// " item.padding(left+right)=" + itemPadding + " ivWidth=" +
		// newImageViewWidth + " ivHeight=" + newImageViewHeight);
	}

	private void bindText() {
		tv.setText(String.valueOf(getItem().getData().getId()));
		tv.setTextColor(Color.WHITE);
	}

	private void bindImage() {
		if (oldItem == getItem().getData()) {
			if (this.bitmap == null) {// 其实也不算是大错，有可能是加载一张很大的图片，但这张图片还没下载得完
				LogUtil.e(TAG, "出大错了*****************************************************");
			} else {
				return;
			}
		}
		if (task != null)
			task.cancel(true);
		Shop shop = getItem().getData();
		task = new ImageLoadTask(getMyContext(), "www.baidu.com", shop.getImgUrl());
		task.setCallBack(this);
		task.setImageWidth(itemWidth);
		task.execute();
	}

	@Override
	protected void onResetViews() {

	}

	@Override
	protected void onRecycleItem() {
		clearImageView();
	}

	@Override
	protected void onRefreshView() {
		 
	}

	@Override
	protected void onDestroy() {

	}
	
	private void clearImageView(){
		//TODO 可以设置成默认的图片
		iv.setImageBitmap(null);
		recycleBitmap(bitmap);
	}

	Bitmap bitmap = null;

	@Override
	public void onSuccess(Bitmap t) {
		if (t == null)
			return;
		iv.setImageBitmap(t);
		if (bitmap != null) {
			recycleBitmap(bitmap);
		}
		bitmap = t;
	}

	@Override
	public void onException(Exception e) {
		LogUtil.e(TAG, "LoadImageTask Exception:", e);
	}

	private void recycleBitmap(Bitmap bmp) {
		if (bmp == null || bmp.isRecycled())
			return;
		bmp.recycle();
		bmp = null;
	}

	// ======================================
	private final static int columnCount = 2;
	private static int screenWidth = 0;
	private static int itemWidth = 0;

	private static void init(Context context) {
		if (screenWidth == 0) {
			screenWidth = ScreenUtil.getDisplayMetrics(context).widthPixels;
			itemWidth = screenWidth / columnCount;
		}
	}

}
